package org.example.back.controller.user;

import jakarta.servlet.http.HttpSession;
import org.example.back.entity.user.UserMeal;
import org.example.back.service.user.UserMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/meal")
public class UserMealController {

    @Autowired
    private UserMealService userMealService;

    // 添加用餐记录（包含食材列表）
    @PostMapping("/add")
    public Map<String, Object> addMeal(@RequestBody UserMeal meal, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        meal.setUserId(userId);
        userMealService.addUserMealWithFoods(meal);
        return Map.of(
            "success", true,
            "message", "添加成功"
        );
    }

    // 获取某日的用餐记录（含食材）
    @GetMapping("/day")
    public Map<String, Object> getMealsByDate(@RequestParam String date, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        List<UserMeal> meals = userMealService.getMealsByDate(userId, LocalDate.parse(date));
        return Map.of("success", true, "data", meals);
    }
    //获取某天的摄入量
    @GetMapping("/nutritionPercentages/daily")
public ResponseEntity<?> getDailyNutritionPercentagesWithCalories(
        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date, HttpSession session) {
            Integer userId = (Integer) session.getAttribute("userId");
    Map<String, Object> data = userMealService.getDailyNutritionPercentagesWithCalories(userId, date);
    return ResponseEntity.ok(Map.of("success", true, "data", data));
}

    // 获取最近7天的每日用餐汇总
    @GetMapping("/week")
    public Map<String, Object> getWeekSummary(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        List<Map<String, Object>> list = userMealService.getLast7DaysSummary(userId);
        return Map.of("success", true, "data", list);
    }

    // 获取最近30天的每周用餐汇总
    @GetMapping("/month")
    public Map<String, Object> getMonthSummary(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        List<Map<String, Object>> list = userMealService.getLast30DaysSummary(userId);
        return Map.of("success", true, "data", list);
    }

    // 删除指定日期的所有用餐记录
    @DeleteMapping("/delete/date")
    public Map<String, Object> deleteMeal(@RequestParam String date, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return Map.of("success", false, "message", "未登录");
        }
        userMealService.deleteUserMeal(userId, LocalDate.parse(date));
        return Map.of("success", true, "message", "删除成功");
    }


    // 删除指定日期和餐次的用餐记录
@DeleteMapping("/delete")
public Map<String, Object> deleteMeal(
        @RequestParam String date,
        @RequestParam String mealTime,
        HttpSession session) {
    Integer userId = (Integer) session.getAttribute("userId");
    if (userId == null) {
        return Map.of("success", false, "message", "未登录");
    }

    boolean deleted = userMealService.deleteMealByUserDateTime(userId, LocalDate.parse(date), mealTime);
    if (deleted) {
        return Map.of("success", true, "message", "删除成功");
    } else {
        return Map.of("success", false, "message", "未找到对应的用餐记录");
    }
}
}
